#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2019 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.

'''
NOTE: 这个文件模仿一个进程中的多个线程, 使用操作系统提供的锁来实现互斥访问共享资源.
'''

from simulate_os_cas_lock import *


g_sum = 0
sum_lock = MySpinLock()


def sum_func(start, end):
    for i in range(start, end):
        sum_lock.lock()
        g_sum += i
        sum_lock.release()


def main():

    range = 20

    for i in range(5):
        start = i * range
        end   = (i + 1) * range

        # 这5个线程, 并发执行sum_func函数
        create_thread(sum_func, start, end)
